<template>
  <view class="my-settle-container">
    <!-- 全选区域 -->
    <label class="radio" @click="changeAllState">
      <radio color="#c00000" :checked="isFullCheck" /><text>全选</text>
    </label>
    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{checkedGoodsAmount}}</text>
    </view>
    <!-- 结算按钮 -->
    <view class="btn-settle" @click="settlement">
      结算{{checkedCount}}
    </view>
  </view>
</template>

<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    name: "my-settle",
    data() {
      return {
        // 倒计时的秒数
        time: 3,
        timer: null
      };
    },
    computed: {
      ...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
      ...mapGetters('m_user', ['addstr', 'token']),
      ...mapState('m_user', ['token']),
      ...mapState('m_cart', ['cart','payGoods']),
      // 判断是否全选
      isFullCheck() {
        return this.total == this.checkedCount
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateAllGoodsState', 'removeGoodsById', 'addPayCart']),
      ...mapMutations('m_user', ['updateRedirectInfo']),
      // 修改购物车中所有商品的选中状态
      changeAllState() {
        this.updateAllGoodsState(!this.isFullCheck)
      },
      // 点击了结算按钮
      settlement() {
        // 先判断有没有勾选结算商品
        if (!this.checkedCount) return uni.$showMsg('请选择要结算的商品!')
        // 判断是否填写了收货地址
        if (!this.addstr) return uni.$showMsg('请选择收货地址!')
        // 判断是否登录了
        // if(!this.token) return uni.$showMsg('请先登录!')
        if (!this.token) return this.delayNavigate()
        uni.$showMsg("结算成功!")
        // 微信支付功能   无权限
        this.payOrder()
      },
      // 微信支付

      async payOrder() {
      let price=this.checkedGoodsAmount
        // 创建订单
        let payGoods = this.cart.filter(x => {
          if (x.goods_state) {
            this.removeGoodsById(x.goods_id)
            return x
          }
        })
        const orderInfo = {
          order_number: +new Date(),
          order_time: this.getCurrentTime(),
          order_price: price,
          consignee_addr: this.addstr,
          goods: payGoods
        }
        this.addPayCart(orderInfo)
        console.log(orderInfo)
        uni.showToast({
          title: '支付完成！',
          icon: 'success'
        })
    },
     
     getCurrentTime() {
        var date = new Date();//当前时间
        var year = date.getFullYear() //返回指定日期的年份
        var month = this.repair(date.getMonth() + 1);//月
        var day = this.repair(date.getDate());//日
        var hour = this.repair(date.getHours());//时
        var minute = this.repair(date.getMinutes());//分
        var second = this.repair(date.getSeconds());//秒
        
        //当前时间 
        var curTime = year + "-" + month + "-" + day
                + " " + hour + ":" + minute + ":" + second;
        return curTime;
    },
    //补0
     repair(i){
        if (i >= 0 && i <= 9) {
            return "0" + i;
        } else {
            return i;
        }
    },

    // 展示倒计时的提示
    showTips(n) {
      uni.showToast({
        icon: 'none',
        title: '请先登录后再结算' + n + '秒后自动跳转到登录页',
        // 开启遮罩层后,按钮在toast显示期间不能被点击
        mask: true,
        duration: 1500
      })
    },
    // 倒计时
    delayNavigate() {
      this.time = 3
      this.showTips(this.time)
      this.timer = setInterval(() => {
        this.time--
        // 进行清除定时器 后跳转到登录页
        if (this.time <= 0) {
          clearInterval(this.timer)
          uni.switchTab({
            url: '../../pages/my/my',
            success: () => {
              this.updateRedirectInfo({
                // 跳转的方式
                openType: 'switchTab',
                // 从哪个页面跳转过去的
                from: '/pages/cart/cart'
              })
            }
          })
          return
        }
        this.showTips(this.time)
      }, 1000)
    }
  },
  }
</script>

<style lang="scss">
  .my-settle-container {
    // 底部固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 5px;
    font-size: 14px;

    .radio {
      display: flex;
      align-items: center;
    }

    .amount {
      color: #c00000;
    }

    .btn-settle {
      height: 50px;
      min-width: 100px;
      background-color: #c00000;
      color: white;
      line-height: 50px;
      text-align: center;
      padding: 0 10px;
    }
  }
</style>
